package com.example.signupservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commonservice.Common.Result;
import com.example.signupservice.entity.ActivitySignup;
import com.example.signupservice.feign.ActivityFeignClient;
import com.example.signupservice.feign.UserFeignClient;
import com.example.signupservice.mapper.ActivitySignupMapper;
import com.example.signupservice.service.ActivitySignupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ActivitySignupServiceImpl extends ServiceImpl<ActivitySignupMapper, ActivitySignup>
    implements ActivitySignupService {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public boolean signUp(Long activityId, Long volunteerId) {
        // 校验是否已报名
        QueryWrapper<ActivitySignup> wrapper = new QueryWrapper<>();
        wrapper.eq("activity_id", activityId)
               .eq("volunteer_id", volunteerId);
        if (count(wrapper) > 0) {
            return false; // 已报名，不重复添加
        }

        ActivitySignup signup = new ActivitySignup();
        signup.setActivityId(activityId);
        signup.setVolunteerId(volunteerId);
        signup.setStatus(1); // 默认状态：已报名
        return save(signup);
    }

    @Override
    public IPage<ActivitySignup> pageSignups(Integer current, Integer size, Map<String, Object> params) {
        Page<ActivitySignup> page = new Page<>(current, size);
        QueryWrapper<ActivitySignup> wrapper = new QueryWrapper<>();

        // 动态条件查询
        if (params.containsKey("activityId") && params.get("activityId") != null) {
            wrapper.eq("activity_id", params.get("activityId"));
        }
        if (params.containsKey("volunteerId") && params.get("volunteerId") != null) {
            wrapper.eq("volunteer_id", params.get("volunteerId"));
        }
        if (params.containsKey("status") && params.get("status") != null) {
            wrapper.eq("status", params.get("status"));
        }

        IPage<ActivitySignup> result = page(page, wrapper);

        // 获取活动名称（添加异常处理）
        result.getRecords().forEach(signup -> {
            try {
                Result<String> nameResult = activityFeignClient.getActivityName(signup.getActivityId());
                if(nameResult != null && nameResult.isSuccess()) {
                    signup.setActivityName(nameResult.getData());
                }
            } catch (Exception e) {
                signup.setActivityName("未知活动");
            }
        });

        // 获取志愿者姓名
        result.getRecords().forEach(signup -> {
            // 获取活动名称
            Result<String> activityResult = activityFeignClient.getActivityName(signup.getActivityId());
            if(activityResult.isSuccess()) {
                signup.setActivityName(activityResult.getData());
            }

            // 获取志愿者名称
            Result<String> volunteerResult = userFeignClient.getVolunteerName(signup.getVolunteerId());
            if(volunteerResult.isSuccess()) {
                signup.setVolunteerName(volunteerResult.getData());
            }
        });
        return result;
    }
}
